package com.fezs.excel.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认excel返回对象
 */
@Getter
@Setter
public class DefautExcelResult<T> implements ExcelResult<T> {

    /**
     * 数据对象list
     */
    private List<T> list;

    /**
     * 错误信息列表
     */
    private List<String> errorList;

    public DefautExcelResult() {
        this.list = new ArrayList<>();
        this.errorList = new ArrayList<>();
    }

    public DefautExcelResult(List<T> list, List<String> errorList) {
        this.list = list;
        this.errorList = errorList;
    }

    public DefautExcelResult(ExcelResult<T> excelResult) {
        this.list = excelResult.getList();
        this.errorList = excelResult.getErrorList();
    }

    @Override
    public boolean success() {
        int errorCount = errorList.size();
        if (errorCount > 0) {
            return false;
        }
        int successCount = list.size();
        return successCount > 0;
    }

    /**
     * 获取导入回执
     *
     * @return 导入回执
     */
    @Override
    public String getAnalysis() {
        int successCount = list.size();
        int errorCount = errorList.size();
        if (successCount == 0) {
            return "读取失败, 未解析到数据!";
        } else {
            if (errorCount == 0) {
                return StrUtil.format("全部读取成功! 共{}条", successCount);
            } else {
                if (errorCount > 20) {
                    List<String> list = CollUtil.sub(errorList, 0, 20);
                    return CollUtil.join(list, StrUtil.COMMA) + ".....";
                }
                return CollUtil.join(list, StrUtil.COMMA);
            }
        }
    }
}
